﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Social.Facebook;
using SocialNetworks.Config;

public partial class Pages_Dashboard_Connect : System.Web.UI.Page, ICrumbedPage
{
    public int AccountID
    {
        get
        {
            int temp;
            if (!int.TryParse(Request["ID"], out temp))
                RedirectHome();
            return temp;
        }
    }

    public bool HasFb = false;
    private BO.Account account;

    private void RedirectHome()
    {
        Response.Redirect("/default.aspx");
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        account = BLL.Accounts.GetByID(AccountID);
        if (account == null)
            RedirectHome();

        ltrName.Text = account.Name;
        TailName = string.Format("{0} social networks connection", account.Name);
        hlAccount.NavigateUrl = AppParams.URLs.AccountPageUrl(account.ID);
		pageSelector.Account = account;
        if (!IsPostBack)
            SetDisplay();
    }

    private void SetDisplay()
    {
        if (!account.HasFacebook)
            SetSocialNetworkConnectDisplayMode(imgFb, lbtnRemoveFb, false);
        else
            SetSocialNetworkConnectDisplayMode(imgFb, lbtnRemoveFb, true);
        var f = new Social.Facebook.FacebookGraph(new FbGraphConfig(FacebookExtededPermissions.OffileAccess, FacebookExtededPermissions.PublishStream, FacebookExtededPermissions.ManagePages));
        lbtnFacebook.OnClientClick = string.Format("window.open('{0}','fb','width=550,height=400,top=100,left=250'); return false;", f.ConnectUrl);
		

        if (!account.HasTwitter)
            SetSocialNetworkConnectDisplayMode(imgTw, lbtnRemoveTw, false);
        else
            SetSocialNetworkConnectDisplayMode(imgTw, lbtnRemoveTw, true);
        var t = new Social.Twitter.TwitterAPI(new TwitterConfig());
        lbtnTwitter.OnClientClick = string.Format("window.open('{0}','fb','width=550,height=400,top=100,left=250'); return false;", t.ConnectUrl);

        if (!account.HasYoutube)
            SetSocialNetworkConnectDisplayMode(imgYt, lbtnRemoveYt, false);
        else
            SetSocialNetworkConnectDisplayMode(imgYt, lbtnRemoveYt, true);
        var y = new Social.YouTube.YouTubeApi(new YouTubeConfig());
        lbtnYt.OnClientClick = string.Format("window.open('{0}','fb','width=550,height=400,top=100,left=250'); return false;", y.ConnectUrl);

		//if (!account.HasWordpress)
		//    SetSocialNetworkConnectDisplayMode(ImgWp, lbtnRemoveWp, false); 
		//else
		//    SetSocialNetworkConnectDisplayMode(ImgWp, lbtnRemoveWp, true);
		//var w = new Social.Wordpress.Wordpress(new WordPressConfig(account));
		//lbtnWp.OnClientClick = string.Format("ShowWrdPressDialog(); return false;");//string.Format("window.open('{0}','fb','width=550,height=400,top=100,left=250'); return false;", w.ConnectUrl);
    }
    /// <summary>
    /// This method will set connect to social network button display mode
    /// </summary>
    /// <param name="image">the green connected button </param>
    /// <param name="button">the remove linkButton</param>
    /// <param name="isConnected">determined is the user connected to the network to display the image and link button or not</param>
    private void SetSocialNetworkConnectDisplayMode(Image image, LinkButton button, bool isConnected)
    {
        switch (isConnected)
        {
            case true:
                image.Attributes.CssStyle.Add(HtmlTextWriterStyle.Display, string.Empty);
                button.Attributes.CssStyle.Add(HtmlTextWriterStyle.Display, string.Empty);
                break;
            case false:
                image.Attributes.CssStyle.Add(HtmlTextWriterStyle.Display, "none");
                button.Attributes.CssStyle.Add(HtmlTextWriterStyle.Display, "none");
                break;
            default:
                break;
        }
    }

    protected void lbtnRemove_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandArgument.ToString())
        {
            case "fb":
				account.FbToken = "";
				account.FbUid = null;
				account.FbPageID = null;
				account.FbPageToken = "";
				break;
            case "tw":
                account.TwToken = "";
                account.TwSecret = "";
                break;
            case "yt": account.YtToken = "";
                break;
            case "wp":
                account.WpPassword = string.Empty;
                account.WpUrl = string.Empty;
                account.WpUsername = string.Empty;
                break;
            default:
                return;
        }

        account.Update();
        SetDisplay();
    }

	//protected void lbtnWpArgumentsSubmit_Click(object sender, EventArgs e)
	//{
	//    account.WpUsername = hfWpUserName.Value;
	//    account.WpPassword = hfWpPassword.Value;
	//    account.WpUrl = hfWpBlogUrl.Value;
	//    account.Update();
	//    SetDisplay();
	//}

    #region ICrumbedPage Members

    public int Level
    {
        get { return 4; }
    }

    public string TailName { get; set; }

    #endregion ICrumbedPage Members
}